/*
 * Copyright 2017 Baiyyy
 * @(#)RealtimeReportInstruction.java
 */

package com.byyy.ccts.modules.cc.handle.istr.v12;

import com.byyy.ccts.core.utils.DateUtils;
import com.byyy.ccts.core.utils.HexUtils;
import com.byyy.ccts.core.utils.InstructionUtils;
import com.byyy.ccts.modules.cc.Global;
import com.byyy.ccts.modules.cc.entity.ccts.TSHumiture;
import com.byyy.ccts.modules.cc.entity.istr.InstructionMdl;
import com.byyy.ccts.modules.cc.entity.istr.v12.RealTimeReportMdl;
import com.byyy.ccts.modules.cc.handle.istr.IHandler;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Arrays;

/**
 * 178A协议V1.2 实时消息上报处理类
 * Created by DELL on 2018-4-26.
 */
@Service("realTimeRptIstrHdl")
public class RealTimeRptIstrHdl extends IHandler {

    /**
     * 实时消息上报指令解析
     * 1.校验(已提出共通)
     * 2.解析实时消息上报指令
     * 3.将数据插入数据库
     * 4.应答(已提出共通)
     * @param instructions
     * @return
     */
    @Override
    public byte[] handle(byte[] instructions) {
        logger.debug("实时数据V1.2 开始执行");
        // 解析实时消息上报指令
        InstructionMdl istrMdl = this.generateInstructionModel(instructions);
        RealTimeReportMdl rtReport = new RealTimeReportMdl();
        rtReport.setSequenceNum(istrMdl.getSequenceNum());
        rtReport.setDeviceId(istrMdl.getDeviceId());

        // 补传标志
        rtReport.setSupplementFlag(instructions[15]);

        // GMS时间
        byte[] gmsTime = new byte[6];
        for (int i = 0; i < 6; i++) {
            gmsTime[i] = instructions[16 + i];
        }
        rtReport.setGsmTime(gmsTime);

        // 基站小区号
        byte[] baseStationAreaNum = new byte[4];
        for (int i = 0; i < 4; i++) {
            baseStationAreaNum[i] = instructions[22 + i];
        }
        rtReport.setBaseStationAreaNum(baseStationAreaNum);

        // 基站编号
        byte[] baseStationNum = new byte[4];
        for (int i = 0; i < 4; i++) {
            baseStationNum[i] = instructions[26 + i];
        }
        rtReport.setBaseStationNum(baseStationNum);

        // 信号量
        rtReport.setSemaphore(instructions[30]);
        // 电池电量
        rtReport.setElectric(instructions[31]);
        // 温度符号
        rtReport.setTempSymbol(instructions[32]);
        // 温度整数部分
        rtReport.setTempeInt(instructions[33]);
        // 温度小数部分
        rtReport.setTempDec(instructions[34]);
        // 环境温度符号
        rtReport.setEnvTempSymbol(instructions[35]);
        // 环境温度整数部分
        rtReport.setEnvTempeInt(instructions[36]);
        // 环境温度小数部分
        rtReport.setEnvTempeDec(instructions[37]);
        // 湿度值
        rtReport.setHumidity(instructions[38]);
        // 报警值
        byte[] warn = new byte[2];
        warn[0] = instructions[39];
        warn[1] = instructions[40];
        rtReport.setWarn(warn);

        this.insert(rtReport);

        logger.debug("实时数据V1.2 执行结束");
        // 生成应答信息，返回
        return InstructionUtils.generateCommResponse(rtReport, Global.ISTR_REALTIME_REPORT);
    }

    /**
     *  将实时数据插入数据库中
     */
    private void insert(RealTimeReportMdl model) {
        TSHumiture tsHumiture = new TSHumiture();
        float temp = 0;
        tsHumiture.setDeviceSn(HexUtils.bytesToHexFun3(model.getDeviceId()));
        tsHumiture.setCollectTime(new Timestamp(DateUtils.structure(
                model.getGsmTime()[0], model.getGsmTime()[1] - 1,
                model.getGsmTime()[2], model.getGsmTime()[3],
                model.getGsmTime()[4], model.getGsmTime()[5]).getTime()));

        // 设置温度值,注意零下的情况
        temp = Float.valueOf((int) model.getTempeInt() + "." + (int) model.getTempDec());
        if (!(Global.OTH_TEMP_SYMBOL_PLUS == model.getTempSymbol())) {
            temp = -temp;
        }
        tsHumiture.setTemperature(temp);
        tsHumiture.setSemaphore(model.getSemaphore());
        tsHumiture.setElectric(model.getElectric());
        tsHumiture.setHumidity(model.getHumidity());
//        tsHumiture.setCellId(HexUtils.bytes2int(model.getBaseStationNum()));
//        tsHumiture.setLac(HexUtils.bytes2int(model.getBaseStationAreaNum()));

        this.insertHumiture(Arrays.asList(tsHumiture));
    }

}
